package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import biz.appframe.Module;
import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.sql.JPASQLQuery;
import com.mysema.query.sql.SQLQuery;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;

import java.util.List;

@Service
@Transactional
public class TechnicalTitleService{

	@Autowired
	private TechnicalTitleDao technicalTitleDao;
	
	private PeopleDao peopleDao;
	
	private QTechnicalTitle $ = QTechnicalTitle.technicalTitle;
	
	private QPeople $_ = QPeople.people;
	
	@Autowired
	public void setTechnicalTitleDao(TechnicalTitleDao technicalTitleDao) {
		this.technicalTitleDao = technicalTitleDao;
	}
	
	@Autowired
	public void setPeopleDao(PeopleDao peopleDao) {
		this.peopleDao = peopleDao;
	}
	
	public List<TechnicalTitle> findAll(){
		return ListUtil.toList(technicalTitleDao.findAll());
	}
				
	public List<TechnicalTitle> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(technicalTitleDao.findAll(condition));
	}		

	public Page<TechnicalTitle> findAll(
			String name,
			Double workTimeFactor,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
			if(null != workTimeFactor){
				condition.and($.workTimeFactor.eq(workTimeFactor));
			}
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return technicalTitleDao.findAll(condition, pageRequest);
	}
	
	
	
	public TechnicalTitle getTechnicalTitle(Long id){
		return technicalTitleDao.getOne(id);
	}
	
	public void deleteTechnicalTitle(Long id) throws CannotDelException{
		if (this.findPeopleByTech(id)) {
			throw new CannotDelException("技术职位已经被人员引用，请先删除人员信息或修改人员的引用！");
		} else {
			this.technicalTitleDao.delete(id);
		}
	}
	
	private boolean findPeopleByTech(Long techId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.technicalTitle.id.eq(techId));
		return this.peopleDao.count(condition) > 0;
	}
	
	public void saveTechnicalTitle(TechnicalTitle technicalTitle) {
		if (null == technicalTitle.getOrderNumber())
			technicalTitle.setOrderNumber(this.getOrderNumber());
		if(null == technicalTitle.getWorkTimeFactor())
			technicalTitle.setWorkTimeFactor(1.00);
		technicalTitleDao.save(technicalTitle);
	}
	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<TechnicalTitle> technicalTitles = this.technicalTitleDao.findAll(condition,pageRequest);
		if(technicalTitles.getTotalElements() > 0){
			TechnicalTitle technicalTitle = technicalTitles.getContent().get(0);
			return technicalTitle.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		TechnicalTitle currentModule = this.technicalTitleDao.findOne(id);
		TechnicalTitle targetModule = this.technicalTitleDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.technicalTitleDao.save(currentModule);
		this.technicalTitleDao.save(targetModule);
		return true;
	}

	public TechnicalTitle findOne(Long id) {
		return this.technicalTitleDao.findOne(id);
	}
	


}
		
